package accounting.action;

import accounting.model.Income;
import accounting.service.IncomeService;
import com.opensymphony.xwork2.ActionSupport;
import manage.model.IncomeType;
import manage.service.IncomeTypeService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuweigui
 */
@SuppressWarnings("serial")
public class IncomeAction extends ActionSupport {

    private List<Income> incomes = new ArrayList<Income>();

    private String incomeTypesJSON;

    private IncomeService incomeService;
    private IncomeTypeService incomeTypeService;
    private Income income = new Income();
    //for ajax call 
    //result="success" if successful, otherwise error message content
    private String result;

    public String delete() {
        incomeService.remove(income.getId());
        return SUCCESS;
    }

    public String execute() {
        incomes = incomeService.findAll();

        List<IncomeType> incomeTypes = incomeTypeService.findAll();
        StringBuilder sb = new StringBuilder();
        sb.append("incomeTypes:[");
        for (IncomeType incomeType : incomeTypes) {
            sb.append(incomeType.toJSON()).append(",");
        }
        sb.append("]");

        return SUCCESS;
    }

    public String add() {
        incomeService.add(income);
        return SUCCESS;
    }

    public IncomeTypeService getIncomeTypeService() {
        return incomeTypeService;
    }

    public void setIncomeTypeService(IncomeTypeService incomeTypeService) {
        this.incomeTypeService = incomeTypeService;
    }

    public String getIncomeTypesJSON() {
        return incomeTypesJSON;
    }

    public void setIncomeTypesJSON(String incomeTypesJSON) {
        this.incomeTypesJSON = incomeTypesJSON;
    }

    public List<Income> getIncomes() {
        return incomes;
    }

    public void setIncomes(List<Income> incomes) {
        this.incomes = incomes;
    }

    public IncomeService getIncomeService() {
        return incomeService;
    }

    public void setIncomeService(IncomeService incomeService) {
        this.incomeService = incomeService;
    }

    public Income getIncome() {
        return income;
    }

    public void setIncome(Income income) {
        this.income = income;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }


}
